package com.my.handler;

import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Author hujf
 * @Date 2021/10/25 10:14
 * @Description 处理http
 */
public class HttpHandler extends IdleStateAwareChannelUpstreamHandler {

    private CommandManager commandManager = CommandManager.instance(null);
    private Logger log = LoggerFactory.getLogger(HttpHandler.class);


    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        Object message = e.getMessage();
        if(message instanceof HttpRequest) {
            handleHttpRpc(e.getChannel(), (HttpRequest) message);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        log.error(e.toString());
    }

    private void handleHttpRpc(Channel channel, HttpRequest message){
        //这里去处理解析http的请求
        String route = message.getUri();

        //根据不同的路径 去执行不同的请求
        HttpRpc hrpc =commandManager.findHandler(route);

        hrpc.execute();

    }
}
